<template>
  <el-tabs
    type="border-card"
    v-model="activeName"
    @tab-click="handleClick"
    class="left_tree_tap"
  >
    <div class="sjtj_ts"><i class="el-icon-warning-outline"></i>双击即添加</div>
    <el-tab-pane label="指标" name="first" style="padding: 0">
      <el-container>
        <el-header class="height_auto pdbt">
          <el-row :gutter="6" class="bk_padding_le_rg">
            <el-col :span="12">
              <vxe-input
                id="treeSreach"
                v-model="searchVal"
                placeholder="指标名称"
                type="search"
                size="mini"
                prefix-icon="fa fa-sitemap"
                class="input_bk"
                @search-click="refresh(searchVal)"
                @keyup.enter.native="refresh(searchVal)"
              />
            </el-col>
            <el-col :span="12" style="text-align: right">
              <el-button type="text" icon="el-icon-refresh" size="mini" @click="refresh"
                >刷新
              </el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main>
          <ps-tree
            ref="tbdwTree"
            :tree-comp-data="tbdwlTreeData"
            @createRightMenu="createRightMenu"
            @refresh="refresh"
            @onTreeClick="treeClick"
          />
        </el-main>
        <el-footer v-show="csShow">
          <div class="xuanlexing">
            <el-container class="align_box">
              <el-main>
                <el-select
                  v-model="value"
                  placeholder="请选择常数"
                  size="mini"
                  clearable
                  class="select_box"
                >
                  <el-option
                    v-for="(item, index) in field181Options"
                    :key="index"
                    :label="item.tcCtNm"
                    :value="item.tcCtCd"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-main>
              <el-aside width="90px" style="line-height: 20px; padding: 0 0px 0 8px">
                <el-button
                  type="primary"
                  @click="sumClick"
                  icon="el-icon-circle-plus-outline"
                  round
                  plain
                  size="mini"
                  >添加</el-button
                >
              </el-aside>
            </el-container>
          </div>
        </el-footer>
      </el-container>
    </el-tab-pane>
    <el-tab-pane label="函数" name="second">
      <function-Selector @funClick="funClick" />
    </el-tab-pane>
  </el-tabs>
</template>
<script>
import request from "@/utils/requestutil";
import { getRealUrl, syncRequest } from "@/utils/httpRequet";
import $ from "jquery";
import treeExhide from "@/components/ztree/lib/jquery.ztree.exhide.min.js";
import dicPanel from "@/hr/utils/dicPanel";
import { options } from "runjs";
import psTree from "@/components/psTree/psTree";
import { isNull } from "xe-utils/methods";
import functionSelector from "@/components/FunctionSelector/FunctionSelector";
export default {
  components: {
    psTree,
    dicPanel,
    functionSelector,
  },
  data() {
    return {
      lable: "",
      ywValue: "",
      searchVal: "",
      rwValue: "",
      tableID: "",
      csShow: false,
      searchPara: "",
      treeParam: {},
      field181Options: [],
      value: "",
      orgschval: "",
      bzcode: "TBJJ",
      tbdwlTreeData: {
        treeConfigKey: "BNSH_ZB_TREE",
        // treeConfigKey:'ZBGL_TI_INDEX_TREE',
        treeBuildConfig: {
          iconMap: {
            jg: "tree-icon jg",
            zbabc: "tree-icon zbabc",
          },
          beforeDrop: this.beforeDrop,
          dealTreaparam: this.dealTreaparam,
          dataFilter: this.datafilter,
          beforeDblClick: this.beforeDblClick,
          beforeDrag:this.beforeDrag,
          isEdit: true,
          editShowType: 2,
          //设置树的复选框
          // options:{
          //   check: {
          //     chkboxType: { 'Y': 'ps', 'N': 's' },
          //     enable: true
          //   },
          // }
        },
      },
      activeName: "first",
    };
  },
  props: {
    leftTreeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    leftTreeData: {
      handler(data) {
        this.ywValue = data.ywValue;
        this.rwValue = data.rwValue;
        this.tableID = data.treeID;
        this.$refs.tbdwTree.refresh();
      },
      deep: true,
    },
  },
  methods: {
    //添加按钮事件
    sumClick() {
      this.$emit("sumClick", this.value);
    },
    //函数双击事件
    funClick(val) {
      this.val = val;
      this.$emit("hsClick", val);
    },
    handleClick(tab, event) {
      //console.log(tab);
    },
    createRightMenu(params) {
      params.items = [];
    },
    datafilter(treeid, pnode, resdata) {
      let t = this;
      let tree = [];
      if (resdata && resdata.data && resdata.data.length > 0) {
        t.$_.forEach(resdata.data, (item, val) => {
          if (item.leaf == "0") item.isParent = true;
        });
      } else {
        if (pnode) pnode.isParent = false;
      }
      if (pnode == undefined) {
        //查询根节点
        if (t.ywValue != "" && t.rwValue != "" && t.tableID != "") {
          let res = syncRequest({
            url: "bnshZbTreeData",
            method: "post",
            async: false,
            //data:{bzcode:t.ywValue,ttId:t.rwValue,tsCd:t.tableID,ttVerN:t.ttVerN}
            data: {
              bzcode: t.ywValue,
              ttId: t.rwValue,
              tsCd: t.tableID,
              searchPara: this.searchPara,
            },
          });
          let strTreeData = eval(res.data);
          //if(strTreeData.length > 0){
          tree = strTreeData;
          // }else{
          //    this.$message.warning("该表单下没有相关指标，请刷新或者联系管理员！");
          // }
        }
        t.$_.forEach(tree, (item, val) => {
          item.iconcls = "jg";
          if (item.children != undefined && item.children.length > 0) {
            t.$_.forEach(item.children, (strVal, val) => {
              strVal.iconcls = "zbabc";
            });
          }
        });
        resdata.data = tree;
      }
      return resdata.data;
    },
    dealTreaparam(treeid, node) {
      const _that = this;
      if (node == undefined || node == "") {
        //return {p0:_that.ywValue,p1:_that.rwValue,p2:_that.tableID}
        return { p0: "ROOT", p1: "ROOT", p2: "ROOT" };
      }
      // else{
      //   return {p0:_that.ywValue,p1:_that.rwValue,p2:_that.tableID};
      // }
    },
    beforeDrop(treeId, treeNodes, targetNode) {
      return false;
    },
    beforeDrag(){
      return false;
    },
    //指标双击事件
    beforeDblClick(treeId, treeNode) {
      if (treeNode.tiINm != "undefined") {
        this.$emit("zbClick", "@" + treeNode.tiICd + " ");
      }
      this.$emit("afterZbDblClick", treeNode);
      return false;
    },
    //加载指标树
    // zbLodeTree(ywValue,rwValue,tableID,ttVerN){
    //   this.ywValue=ywValue;
    //   this.rwValue=rwValue;
    //   this.tableID=tableID;
    //   this.ttVerN=ttVerN;
    //   try {
    //     this.$refs.tbdwTree.refresh();
    //   } catch (error) {
    //     console.log("刷新错误");
    //   }
    // },
    zbLodeTree(ywValue, rwValue, tableID) {
      this.ywValue = ywValue;
      this.rwValue = rwValue;
      this.tableID = tableID;
      this.searchPara = "";
      this.searchVal = "";
      try {
        this.$refs.tbdwTree.refresh();
      } catch (error) {
        console.log("刷新错误");
      }
    },
    refresh(node) {
      console.log(node);
      this.searchPara = node;
      this.$refs.tbdwTree.refresh();
    },
    treeClick(e, treeid, node) {
      let _t = this;
      let tiIRtp = node.tiIRtp;
      if (isNull(tiIRtp) || tiIRtp == undefined || tiIRtp == "") {
        _t.field181Options = [];
        _t.value = "";
        _t.csShow = false;
        return false;
      } else {
        request({
          url: "bnshXgflData",
          method: "post",
          params: { bzcode: _t.ywValue, tiIRtp: tiIRtp },
        }).then(function (data) {
          console.log(data.data);
          if (data.data.length > 0) {
            _t.csShow = true;
            _t.field181Options = data.data;
            _t.value = data.data[0].tcCtCd;
          } else {
            _t.csShow = false;
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.select_box {
  width: 100%;
  padding-left: 8px;
}
.align_box {
  align-items: center;
}
.align_box .el-aside {
  background: #fff;
}
.align_box .el-main {
  overflow: hidden;
}
.tabl {
  width: 300px;
}

.tabl ::v-deep {
  .el-tabs__header {
    margin-bottom: 0px;
    height: 33px;
    .el-tabs__nav-scroll {
      height: 33px;
    }
  }
}
</style>
